package com.dwxt.common.wxpay;

import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class MyWXReqUtil {

    private WXPayConfig myWXPayConfig;

    public MyWXReqUtil(WXPayConfig myWXPayConfig) {
        this.myWXPayConfig = myWXPayConfig;
    }

    /**
     * 构造支付参数
     */
    public SortedMap<String, String> buildPayParams(Map<String, String> params) throws Exception {

        Double price = Double.parseDouble(params.get("price"));
        String notifyUrl = params.get("notifyUrl");
        Integer type = Integer.parseInt(params.get("type"));
        String orderId = params.get("orderId");
        String ip = params.get("ip");

        //根据微信支付api来设置
        SortedMap<String, String> data = new TreeMap<>();
        data.put("appid", myWXPayConfig.getAppID());
        data.put("body", "智慧城市");
        data.put("fee_type", "CNY");                           //默认人民币
        data.put("mch_id", myWXPayConfig.getMchID());         //商户号
        data.put("nonce_str", WXPayUtil.generateNonceStr());   // 随机字符串小于32位
        data.put("notify_url", notifyUrl);                    //回调地址
        data.put("out_trade_no", orderId);   //交易号
        data.put("sign_type", "MD5");
        data.put("spbill_create_ip", ip);//终端ip
        data.put("total_fee", String.valueOf(new Double(price * 100).intValue()));       //订单总金额,分
        if (type == 2) {
            //扫码支付
            data.put("trade_type", "NATIVE");                         //支付场景 APP 微信app支付 JSAPI 公众号支付  NATIVE 扫码支付
        } else if (type == 3) {
            //小程序支付
            data.put("openid", params.get("openid"));
            data.put("trade_type", "JSAPI");
        } else {
            //app支付
            data.put("trade_type", "APP");
        }
        String signature = WXPayUtil.generateSignature(data, myWXPayConfig.getKey());  //签名
        data.put("sign", signature);
        return data;
    }

    /**
     * 构造查询参数
     */
    public SortedMap<String, String> buildQueryParams(String orderId) throws Exception {
        SortedMap<String, String> data = new TreeMap<>();
        data.put("appid", myWXPayConfig.getAppID());
        data.put("mch_id", myWXPayConfig.getMchID());         //商户号
        data.put("out_trade_no", orderId);         //订单ID
        data.put("nonce_str", WXPayUtil.generateNonceStr());   // 随机字符串小于32位
        String signature = WXPayUtil.generateSignature(data, myWXPayConfig.getKey());  //签名
        //System.out.println(signature);
        data.put("sign", signature);
        return data;
    }

    /**
     * 构造退款参数
     */
    public SortedMap<String, String> buildRefundParams(String orderId, Double price) throws Exception {
        //根据微信支付api来设置
        SortedMap<String, String> data = new TreeMap<>();
        data.put("appid", myWXPayConfig.getAppID());
        data.put("mch_id", myWXPayConfig.getMchID());         //商户号
        data.put("nonce_str", WXPayUtil.generateNonceStr());   // 随机字符串小于32位
        data.put("out_refund_no", orderId);//退款id
        data.put("out_trade_no", orderId);   //交易号id
        data.put("refund_fee", String.valueOf(new Double(price * 100).intValue()));
        data.put("total_fee", String.valueOf(new Double(price * 100).intValue()));       //订单总金额,分
        String signature = WXPayUtil.generateSignature(data, myWXPayConfig.getKey());  //签名
        //System.out.println(signature);
        data.put("sign", signature);
        return data;
    }

    public Map<String, String> buildResult(Map<String, String> respData) throws Exception {
        Map<String, String> repData = new HashMap<>();
        String codeUrl = respData.get("code_url");
        if (StringUtils.isNotBlank(codeUrl)) {
            //扫码支付
            repData.put("codeUrl", codeUrl);
        } else if ("JSAPI".equalsIgnoreCase(respData.get("trade_type"))) {
            repData.put("appId", myWXPayConfig.getAppID());
            String time = String.valueOf(System.currentTimeMillis() / 1000);
            repData.put("timeStamp", time);
            repData.put("nonceStr", respData.get("nonce_str"));
            repData.put("package", "prepay_id=" + respData.get("prepay_id"));
            repData.put("signType", "MD5");
            String sign = WXPayUtil.generateSignature(repData, myWXPayConfig.getKey()); //签名
            repData.put("paySign", sign);

        } else {
            //返回给APP端的参数，APP端再调起支付接口
            repData.put("appid", myWXPayConfig.getAppID());
            repData.put("partnerid", myWXPayConfig.getMchID());
            repData.put("prepayid", respData.get("prepay_id"));
            repData.put("package", "Sign=WXPay");
            repData.put("noncestr", respData.get("nonce_str"));
            String time = String.valueOf(System.currentTimeMillis() / 1000);
            repData.put("timestamp", time);
            String sign = WXPayUtil.generateSignature(repData, myWXPayConfig.getKey()); //签名

            repData.put("sign", sign);
        }
        //repData.put("orderId", orderId);
        return repData;
    }


}
